﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
namespace LeHouse.Server.WebApi.Controllers
{
    public class FacilityController : ApiController
    {
        Collateral_FacilityService bll = new Collateral_FacilityService();
        /// <summary>
        /// 建筑公共部位状况的数据添加
        /// </summary>
        /// <param name="s">ajax传过来的需要添加的数据</param>
        /// <returns></returns>       
        [HttpPost]
        [Route("api/Facility/Postinsert")]
        public bool Insert([FromBody] Collateral_Facility parms)
        {
            if(count(parms.T_ReportNumber)>0)
            {
                Collateral_Facility parms1 = new Collateral_Facility();
               // parms1.F_id = Convert.ToInt64(DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond);
                parms1.T_ReportNumber = parms.T_ReportNumber;
                parms1.T_StructureID = parms.T_StructureID;
                parms1.T_TypesID = parms.T_TypesID;
                parms1.T_FacadeID = parms.T_FacadeID;
                parms1.T_CommonalityID = parms.T_CommonalityID;
                parms1.T_ElevatorID = parms.T_ElevatorID;
                parms1.T_SupplyID = parms.T_SupplyID;
                parms1.T_WaterID = parms.T_WaterID;
                parms1.T_HeatingID = parms.T_HeatingID;
                parms1.T_EntranceID = parms.T_EntranceID;
                parms1.T_Else = parms.T_Else;
              //  parms1.F_state = 1;



                //var s=  Request["parms"];
                Collateral_FacilityService insert = new Collateral_FacilityService();
                Guid.NewGuid();
                if (insert.Update(parms1, t => t.T_ReportNumber == parms.T_ReportNumber))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            else
            {
                Collateral_Facility parms1 = new Collateral_Facility();
              //  parms1.F_id = Convert.ToInt64(DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond);
                parms1.T_ReportNumber = parms.T_ReportNumber;
                parms1.T_StructureID = parms.T_StructureID;
                parms1.T_TypesID = parms.T_TypesID;
                parms1.T_FacadeID = parms.T_FacadeID;
                parms1.T_CommonalityID = parms.T_CommonalityID;
                parms1.T_ElevatorID = parms.T_ElevatorID;
                parms1.T_SupplyID = parms.T_SupplyID;
                parms1.T_WaterID = parms.T_WaterID;
                parms1.T_HeatingID = parms.T_HeatingID;
                parms1.T_EntranceID = parms.T_EntranceID;
                parms1.T_Else = parms.T_Else;



                //var s=  Request["parms"];
                Collateral_FacilityService insert = new Collateral_FacilityService();
                Guid.NewGuid();
                if (insert.Insert(parms1) == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
           
        }

        /// <summary>
        /// 根据报告号获取建筑公共部位状况的数据
        /// </summary>
        /// <param name="P_ReportNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Facility/SelectByid")]
        public Collateral_Facility showfacilty([FromUri] string P_ReportNumber)
        {
            if(count(P_ReportNumber)>0)
            {
                Collateral_FacilityService showfa = new Collateral_FacilityService();
                return showfa.Community(P_ReportNumber);
            }
            else
            {
                return null;
            }
         
        }
        /// <summary>
        /// 根据报告号去查询建筑公共部位是否有该条记录
        /// </summary>
        /// <param name="P_ReportNumber"></param>
        /// <returns></returns>
        public int count(string P_ReportNumber)
        {
            return bll.count(P_ReportNumber);
        }
    }
}
